package com.yc.zyz.servlet;

import com.yc.zyz.entity.User;
import com.yc.zyz.service.BizException;
import com.yc.zyz.service.ZyzBiz;
import com.yc.zyz.util.Utils;
import com.yc.zyz.vo.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Company 源辰信息
 *
 * @author navy
 * @date 2024/7/28
 * Email haijunzhou@hnit.edu.cn
 */
@WebServlet("/join")
public class JoinServlet extends HttpServlet {

    ZyzBiz biz = new ZyzBiz();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        User user = (User) req.getSession().getAttribute("loginedUser");
        if(user == null){
            Utils.print(resp,new Result(0, "请先登录系统!", null));
            return;
        }
        Integer uid = user.getId();
        String tid = req.getParameter("tid");
        try {
            biz.join(tid,uid);
            Utils.print(resp,new Result(1, "加入成功!", null));
        } catch (BizException e) {
            Utils.print(resp,new Result(-1, e.getMessage(), null));
        } catch (Exception e) {
            e.printStackTrace();
            Utils.print(resp,new Result(-2, "系统异常,请联系管理员", null));
        }
    }
}
